package tools.shapes;

import java.awt.Graphics;
import app.DrawingCanvas;
import java.awt.Point;

/**
 * Defines how to draw a Line on the DrawingCanvas
 */
public class LineShape extends TwoEndShape {

  /* (non-Javadoc)
   * 
   * Defines how to draw the rectangle with corners at the starting and ending
   * point. The smallest coordinates of the starting and ending positions are
   * used to locate the origin of the rectangular shape.  The absolute value
   * of the differences in the x and y coordinates are used for the width and
   * height respectively.
   * 
   * @see tools.shapes.TwoEndShape#draw(java.awt.Graphics, int, int, int, int)
   */
	
	public void addToStack(DrawingCanvas c, Point start, Point end) {
		LineShape newShape = new LineShape();

		newShape.xMin = start.x;
		newShape.yMin = start.y;
		newShape.xMax = end.x;
		newShape.yMax = end.y;
		
		newShape.setColor(c.getpenColor());
		c.push(newShape);
	}

  public void move(Point startPos, Point newPos){
		/* take difference between the two points and update the coordinates 
		 *   accordingly 
		 **/
		int deltaX = newPos.x - startPos.x;
		int deltaY = newPos.y - startPos.y;

		xMin += deltaX;
		xMax += deltaX;
		yMin += deltaY;
		yMax += deltaY;

  }	// move 

  public void resize(Point startPos, Point newPos){

	/* must find which point it is closest to */

	/* calculate the distances */
	float xMinDist = Math.abs(startPos.x - xMin);
	float xMaxDist = Math.abs(startPos.x - xMax);
	float yMinDist = Math.abs(startPos.y - yMin);
	float yMaxDist = Math.abs(startPos.y - yMax);

	/* conditions */
	if( xMinDist < xMaxDist){
		if ( yMinDist <= yMaxDist ){
			/* change the line point xMin, yMin */
			xMin = newPos.x;
			yMin = newPos.y;
		}else{
			/* change off corner dimensions */
			xMin = newPos.x;
			yMax = newPos.y;
		}
	}else{ // a > b
		xMax = newPos.x;
		if( yMinDist >= yMaxDist ){
			/* change the line point xMax, yMax */		
			yMax = newPos.y;
		}else{ // c < d
			/* change off corner dimensions */
			yMin = newPos.y;
		}
	}

  } // resize
	
	public void draw(DrawingCanvas c) {
		draw(c.getimageBufferGraphics(), xMin, yMin, xMax, yMax);
	}
	
	public void drawOutline(DrawingCanvas c) {
		drawOutline(c.getimageBufferGraphics(), xMin, yMin, xMax, yMax);
	}
	
  public void draw(Graphics g, int x0, int y0, int x1, int y1) {
    g.drawLine(x0, y0, x1, y1);
  }
  
  /* (non-Javadoc)
   * 
   * Implemented like draw().  See above.
   * 
   * @see tools.shapes.TwoEndShape#drawOutline(java.awt.Graphics, int, int, int, int)
   */
  public void drawOutline(Graphics g, int x0, int y0, int x1, int y1) {
    g.drawLine(x0, y0, x1, y1);
  }

}// end public class RectangleShape extends TwoEndShape


